*** Guide to scripts***
There are only TWO scripts in this folder than are directly user-accessed.  The others are called by these two scripts.

QDMdataprocessing.m does the following:

- Reads in a B111 component magnetic field map file
- Makes choice to analyze the remanence (sum of maps taken under reversed biased fields) or induced (1/2 of difference of maps taken under reversed biased fields) map
- Establishes the pixel size of the map
- Establishes an estimated liftoff (sensor-sample distance).  This number is used as an initial guess in the dipole fit code and does not need to be highly accurate
- Allows user to crop a region of interest in the map
- Allows user to choose a reference point in the map as zero magnetic field and subtracts this background value from the map.  
- Allows user to fit a 2D quadratic function to the map and subtract this function from the map. 
- Computes Bz map from the B111 map that has been subjected to the above processing

QDMDipoleFittingBatch.m performs dipole fitting and is described below:

*** Dipole fitting and sample data ***
The data in the ./sample_data_spherule folder is the IRM step for the first QDM demagnetization run, which is tabulated in Table 2, Line 5.  (Declination fitted from this map is offset from tabulated, which has been corrected to the sample coordinate system as used for the SQUID microscope measurements.  Small mismatch in inclination (0.4˚) is likely due to different croppings)

To perform a dipole fit, change the file path below and run:

QDMDipoleFittingBatch('/Users/rogerfu/Dropbox/QDM_moment_magnetometry/R1/Dataverse_code/sample_data_spherule/Bz_uc0.mat',[265,150],50,1,12,0,0)

The data in the ./sample_data_synthetic folder is a synthetic dipole from the DIPOLE set discussed in the paper.  It can be fitted using this code (after pathname change):

QDMDipoleFittingBatch('/Users/rogerfu/Dropbox/QDM_moment_magnetometry/R1/Dataverse_code/sample_data_synthetic/Crop1000.mat',[250,250],100,2,12,0,0)

Argument 1: Name of B_z file that includes, at minimum, (1) a map of B_z values (variable Bz), (2) a step size in meters (variable step), and (3) an initial liftoff guess in meters (variable h)

Argument 2: x,y coordinate of center of region to be fit

Argument 3: Crop size in pixels.  Value given is the 1/2 the length of the square crop region

Argument 4: Downsampling by a factor

Argument 5: Number of optimization steps

Argument 6: True/False: Write results to file

Argument 7: True/False: Plot result of optimization steps for each output parameter

FindDipolairty.m finds the dipolarity parameter as define in Fu et al. (2020) "High-sensitivity moment magnetometry with the quantum diamond microscope", Equation 2. 